package com.gun.management.module.task.controller.app;

import com.gun.management.framework.common.exception.ServiceException;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import com.gun.management.framework.security.core.LoginUser;
import com.gun.management.module.member.api.user.MemberUserApi;
import com.gun.management.module.member.api.user.dto.MemberUserRespDTO;
import com.gun.management.module.task.controller.app.vo.*;
import com.gun.management.module.task.service.taskInfo.TaskInfoService;
import com.gun.management.module.task.service.taskRewardRecord.TaskRewardRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.gun.management.framework.common.pojo.CommonResult.success;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.gun.management.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "用户 APP - 任务")
@RestController
@RequestMapping("/task/taskinfo")
@Validated
public class AppTaskController {

    @Resource
    private TaskInfoService taskInfoService;


    @Resource
    private TaskRewardRecordService taskRewardRecordService;


    @Resource
    private MemberUserApi memberUserApi;


    @GetMapping("/detail")
    @Operation(summary = "任务详情")
    // @PreAuthorize("@ss.hasPermission('raffle:lottery:getLotteryPrizeList')")
    public CommonResult<AppTaskInfoRespVO> detail(@RequestParam("taskId") Long taskId) {
        if (taskId == null) throw new ServiceException(408, "任务id不能为空!");

        return success(taskInfoService.getDetailById(taskId));
    }

    @GetMapping("/getTaskPageList")
    @Operation(summary = "任务列表")
    @PermitAll
    public CommonResult<PageResult<AppTaskInfoRespVO>> getTaskPageList(@Valid AppTaskInfoPageReqVO pageReqVO) {

        return success(BeanUtils.toBean(taskInfoService.getTaskPageList(pageReqVO), AppTaskInfoRespVO.class));
    }


    @PostMapping(value = "/getTaskRewardPointResult/{taskId}")
    @Operation(summary = "任务奖励积分")
    @PermitAll
    /**
     * 任务奖励积分 返回的信息 不对，返回的现在是 抽奖活动的信息。
     *
     * 任务奖励积分 应该传  任务id ，判断当前用户是否具备 奖励资格，如果具备 则 返回 积分已到账，不具备 返回比如 今日已奖励N次，不再奖励。 累计已奖励N次，不在奖励。这类的内容
     */
    public CommonResult<AppTaskRetRespVO> getTaskRewardPointResult(@PathVariable("taskId") Long taskId) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777, "登录信息已失效,请重新登录");

        AppTaskRetRespVO appTaskRetRespVO = taskInfoService.getTaskRewardPointResult(taskId);

        return success(appTaskRetRespVO);
    }

    @PutMapping("/submitToAudit")
    @Operation(summary = "提交审核")
    public CommonResult<Boolean> submitToAudit(@Valid @RequestBody AppTaskRewardRecordSaveReqVO appUpdateReqVO) {
        taskRewardRecordService.updateAppRewardRecord(appUpdateReqVO);
        return success(true).setMsg("已成功提交审核");
    }


    @GetMapping(value = "/getAuditStatus/{taskId}")
    @Operation(summary = "查询审核状态")
    @PermitAll
    /**
     * 任务奖励积分 返回的信息 不对，返回的现在是 抽奖活动的信息。
     *
     * 任务奖励积分 应该传  任务id ，判断当前用户是否具备 奖励资格，如果具备 则 返回 积分已到账，不具备 返回比如 今日已奖励N次，不再奖励。 累计已奖励N次，不在奖励。这类的内容
     */
    public CommonResult<AppTaskAuditInfoRespVO> getAuditStatus(@PathVariable("taskId") Long taskId) {
        LoginUser loginUser = getLoginUser();
        MemberUserRespDTO user = memberUserApi.getUser(getLoginUserId());
        if (user == null) throw new ServiceException(777, "登录信息已失效,请重新登录");

        return success(taskRewardRecordService.getAuditStatus(taskId)).setMsg("获取审核状态结果");
    }

}
